{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 8.1 \u6539\u53d8\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u663e\u793a\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u6539\u53d8\u5bf9\u8c61\u5b9e\u4f8b\u7684\u6253\u5370\u6216\u663e\u793a\u8f93\u51fa\uff0c\u8ba9\u5b83\u4eec\u66f4\u5177\u53ef\u8bfb\u6027\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u6539\u53d8\u4e00\u4e2a\u5b9e\u4f8b\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u53ef\u91cd\u65b0\u5b9a\u4e49\u5b83\u7684 __str__() \u548c __repr__() \u65b9\u6cd5\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Pair:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n\n    def __repr__(self):\n        return 'Pair({0.x!r}, {0.y!r})'.format(self)\n\n    def __str__(self):\n        return '({0.x!s}, {0.y!s})'.format(self)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "__repr__() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5b9e\u4f8b\u7684\u4ee3\u7801\u8868\u793a\u5f62\u5f0f\uff0c\u901a\u5e38\u7528\u6765\u91cd\u65b0\u6784\u9020\u8fd9\u4e2a\u5b9e\u4f8b\u3002\n\u5185\u7f6e\u7684 repr() \u51fd\u6570\u8fd4\u56de\u8fd9\u4e2a\u5b57\u7b26\u4e32\uff0c\u8ddf\u6211\u4eec\u4f7f\u7528\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u663e\u793a\u7684\u503c\u662f\u4e00\u6837\u7684\u3002\n__str__() \u65b9\u6cd5\u5c06\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 str() \u6216 print() \u51fd\u6570\u4f1a\u8f93\u51fa\u8fd9\u4e2a\u5b57\u7b26\u4e32\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p = Pair(3, 4)\np"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(p)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6211\u4eec\u5728\u8fd9\u91cc\u8fd8\u6f14\u793a\u4e86\u5728\u683c\u5f0f\u5316\u7684\u65f6\u5019\u600e\u6837\u4f7f\u7528\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u8868\u73b0\u5f62\u5f0f\u3002\n\u7279\u522b\u6765\u8bb2\uff0c!r \u683c\u5f0f\u5316\u4ee3\u7801\u6307\u660e\u8f93\u51fa\u4f7f\u7528 __repr__() \u6765\u4ee3\u66ff\u9ed8\u8ba4\u7684 __str__() \u3002\n\u4f60\u53ef\u4ee5\u7528\u524d\u9762\u7684\u7c7b\u6765\u8bd5\u7740\u6d4b\u8bd5\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p = Pair(3, 4)\nprint('p is {0!r}'.format(p))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('p is {0}'.format(p))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u81ea\u5b9a\u4e49 __repr__() \u548c __str__() \u901a\u5e38\u662f\u5f88\u597d\u7684\u4e60\u60ef\uff0c\u56e0\u4e3a\u5b83\u80fd\u7b80\u5316\u8c03\u8bd5\u548c\u5b9e\u4f8b\u8f93\u51fa\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4ec5\u4ec5\u53ea\u662f\u6253\u5370\u8f93\u51fa\u6216\u65e5\u5fd7\u8f93\u51fa\u67d0\u4e2a\u5b9e\u4f8b\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5458\u4f1a\u770b\u5230\u5b9e\u4f8b\u66f4\u52a0\u8be6\u7ec6\u4e0e\u6709\u7528\u7684\u4fe1\u606f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "__repr__() \u751f\u6210\u7684\u6587\u672c\u5b57\u7b26\u4e32\u6807\u51c6\u505a\u6cd5\u662f\u9700\u8981\u8ba9 eval(repr(x)) == x \u4e3a\u771f\u3002\n\u5982\u679c\u5b9e\u5728\u4e0d\u80fd\u8fd9\u6837\u5b50\u505a\uff0c\u5e94\u8be5\u521b\u5efa\u4e00\u4e2a\u6709\u7528\u7684\u6587\u672c\u8868\u793a\uff0c\u5e76\u4f7f\u7528 < \u548c > \u62ec\u8d77\u6765\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('file.dat')\nf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c __str__() \u6ca1\u6709\u88ab\u5b9a\u4e49\uff0c\u90a3\u4e48\u5c31\u4f1a\u4f7f\u7528 __repr__() \u6765\u4ee3\u66ff\u8f93\u51fa\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u7684 format() \u65b9\u6cd5\u7684\u4f7f\u7528\u770b\u4e0a\u53bb\u5f88\u6709\u8da3\uff0c\u683c\u5f0f\u5316\u4ee3\u7801 {0.x} \u5bf9\u5e94\u7684\u662f\u7b2c1\u4e2a\u53c2\u6570\u7684x\u5c5e\u6027\u3002\n\u56e0\u6b64\uff0c\u5728\u4e0b\u9762\u7684\u51fd\u6570\u4e2d\uff0c0\u5b9e\u9645\u4e0a\u6307\u7684\u5c31\u662f self \u672c\u8eab\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def __repr__(self):\n    return 'Pair({0.x!r}, {0.y!r})'.format(self)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u8fd9\u79cd\u5b9e\u73b0\u7684\u4e00\u4e2a\u66ff\u4ee3\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 % \u64cd\u4f5c\u7b26\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def __repr__(self):\n    return 'Pair(%r, %r)' % (self.x, self.y)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}